package com.taskcollection.service;

import com.taskcollection.bean.StudentBean;
import com.taskcollection.util.DBConnection;
import com.taskcollection.util.DBUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by haocongshun on 2016/7/20.
 */
public class StudentService {
    public boolean reg(StudentBean stu){
        System.out.println("进入StudentService");
        return ServiceUtil.reg(stu);
    }
    public StudentBean login(String phone,String passwd){
        Connection conn = DBConnection.getinstance().getConnection();
        String sql = "SELECT * FROM studentinfo WHERE student_phone=? and student_passwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        StudentBean stu = new StudentBean();
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,phone);
            ps.setString(2,passwd);
            rs = ps.executeQuery();
            if(rs.next()){
                stu.setStudent_id(rs.getString("Student_id"));
                stu.setStudent_phone(rs.getString("student_phone"));
                stu.setStudent_name(rs.getString("student_name"));
                stu.setStudent_gender(rs.getString("student_gender"));
                stu.setStudent_contact(rs.getString("student_contact"));
                stu.setStudent_school(rs.getString("student_school"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            DBUtil.closeConnection(conn);
        }
        return stu;
    }
    public boolean checkLogin(String phone,String passwd){
        StudentBean stu = login(phone,passwd);
        System.out.println(phone+"**"+passwd+"**"+stu.getStudent_id());
        if("".equals(stu.getStudent_id())||stu.getStudent_id()==null){
            return false;
        }else{
            return true;
        }
    }
    public boolean checkPhone(String phone){
        return ServiceUtil.checkPhone("studentinfo",phone);
    }
    public StudentBean getById(String id){
        String sql = "select * from studentinfo where Student_id=?";
        Connection conn = DBConnection.getinstance().getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        StudentBean stu = new StudentBean();
        try{
            ps=conn.prepareStatement(sql);
            ps.setString(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                stu.setStudent_id(rs.getString("Student_id"));
                stu.setStudent_phone(rs.getString("student_phone"));
                stu.setStudent_name(rs.getString("student_name"));
                stu.setStudent_gender(rs.getString("student_gender"));
                stu.setStudent_contact(rs.getString("student_contact"));
                stu.setStudent_school(rs.getString("student_school"));
            }
            return stu;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            DBUtil.closeConnection(conn);
        }
        return null;
    }
}
